﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ADList : System.Web.UI.Page
{
    protected int PageIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ADUser"] == null)
        {
            Response.Redirect("Advertisement.aspx");
        }
        this.DataList2.ItemCreated += new DataListItemEventHandler(DataList2_ItemCreated);
        //this.TextBox2.Text = DateTime.Today.AddDays(1).ToString("yyyy-M-d");
        //this.TextBox3.Text = this.TextBox2.Text;
        //this.TextBox4.Text = "1";
        if (!this.IsPostBack)
        {
            this.Bind("Select [ID],[title] from [GowllCommunitys] where [CreateStatue]=0");
            this.BindView("select top 1 [ID],[Title],[URL],[Price],[UserID],[DayPV] from [GowllCommunitys] where [ID]=0");
        }
        this.RadioButton1.InputAttributes["onclick"] = "reban(\"DayBan.aspx?id=" + this.Label4.Text + "&t=1\");";
        this.RadioButton2.InputAttributes["onclick"] = "reban(\"DayBan.aspx?id=" + this.Label4.Text + "&t=2\");";
        this.RadioButton3.InputAttributes["onclick"] = "reban(\"DayBan.aspx?id=" + this.Label4.Text + "&t=3\");";
        //this.RadioButton1.Checked = false;
        //this.RadioButton2.Checked = false;
        //this.RadioButton3.Checked = false;
    }

    private void BindView(string Sql)
    {
        this.DataList2.DataSource = this.SqlDataSource1;
        this.SqlDataSource1.SelectCommand = Sql;
        this.DataList2.DataBind();
    }
    private void Bind(string Sql)
    {
        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        SqlDataAdapter MyDataAdapter = new SqlDataAdapter(Sql, MySqlConnection);
        DataSet ds = new DataSet();
        MyDataAdapter.Fill(ds);
        PagedDataSource PageData = new PagedDataSource();
        PageData.DataSource = ds.Tables[0].DefaultView;
        PageData.AllowPaging = true;
        PageData.PageSize = 24;
        if (this.PageIndex == -1)
        {
            this.PageIndex = PageData.PageCount - 1;
        }
        else if (this.PageIndex <= 0)
        {
            this.PageIndex = 0;
            this.LinkButton2.Visible = false;
        }
        else
        {
            this.LinkButton2.Visible = true;
        }
        if (this.PageIndex >= PageData.PageCount - 1)
        {
            this.LinkButton3.Visible = false;
            this.PageIndex = PageData.PageCount - 1;
        }
        else
        {
            this.LinkButton3.Visible = true;
        }
        PageData.CurrentPageIndex = this.PageIndex;
        DataList1.DataSource = PageData;
        DataList1.DataBind();
        this.Label3.Text = this.PageIndex.ToString();

    }
    void DataList2_ItemCreated(object sender, DataListItemEventArgs e)
    {
        this.Label1.Text = ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString() + "(" + ((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString() + ")社区";
        this.Label4.Text = ((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString();
        int price=(int)((DataRowView)e.Item.DataItem).Row.ItemArray[3];
        int DayPV = (int)((DataRowView)e.Item.DataItem).Row.ItemArray[5];
        float kpv;
        if (DayPV % 1000 == 0)
        {
            kpv = DayPV / 1000;
        }
        else
        {
            kpv = DayPV / 1000 + 1;
        }
        this.TextBox5.Text = kpv.ToString();
        this.Label2.Text = ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString();

        if (price == 100)
        {
            this.RadioButton1.Text = "置顶位广告 20元/千人";
            this.RadioButton2.Text = "中间位广告 10元/千人";
            this.RadioButton3.Text = "权重位推荐广告 10元/千人";
            //this.TextBox5.Text="2";
        }
        else if (price == 500)
        {
            this.RadioButton1.Text = "置顶位广告 30元/千人";
            this.RadioButton2.Text = "中间位广告 20元/千人";
            this.RadioButton3.Text = "权重位推荐广告 20元/千人";
            //this.TextBox5.Text = "3";
        }
        else if (price == 1000)
        {
            this.RadioButton1.Text = "置顶位广告 50元/千人";
            this.RadioButton2.Text = "中间位广告 30元/千人";
            this.RadioButton3.Text = "权重位推荐广告 30元/千人";
            //this.TextBox5.Text = "4";
        }
        else if (price == 3000)
        {
            this.RadioButton1.Text = "置顶位广告 60元/千人";
            this.RadioButton2.Text = "中间位广告 40元/千人";
            this.RadioButton3.Text = "权重位推荐广告 40元/千人";
            //this.TextBox5.Text = "5";
        }
        else
        {
            this.RadioButton1.Text = "置顶位广告 80元/千人";
            this.RadioButton2.Text = "中间位广告 60元/千人";
            this.RadioButton3.Text = "权重位推荐广告 60元/千人";
            //this.TextBox5.Text = "6";
        }
        this.RadioButton1.InputAttributes["onclick"] = "reban(\"DayBan.aspx?id=" + this.Label4.Text + "&t=1\");";
        this.RadioButton2.InputAttributes["onclick"] = "reban(\"DayBan.aspx?id=" + this.Label4.Text + "&t=2\");";
        this.RadioButton3.InputAttributes["onclick"] = "reban(\"DayBan.aspx?id=" + this.Label4.Text + "&t=3\");";
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.BindView("select top 1 [ID],[Title],[URL],[Price],[UserID],[DayPV] from [GowllCommunitys] where [ID]=" + ((LinkButton)sender).CommandName);
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        int.TryParse(this.Label3.Text, out this.PageIndex);
        this.PageIndex--;
        this.Bind("Select [ID],[title] from [GowllCommunitys] where [CreateStatue]=0");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int.TryParse(this.Label3.Text, out this.PageIndex);
        this.PageIndex++;
        this.Bind("Select [ID],[title] from [GowllCommunitys] where [CreateStatue]=0");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string key = this.TextBox1.Text;
        key = key.Replace("'", "").Trim();
        this.Bind("Select [ID],[title] from [GowllCommunitys] where [CreateStatue]=0 and [title] like '%" + key + "%'");
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string errMsg="";
        DateTime dtStart,dtEnd;
        dtStart=dtEnd = DateTime.Now;
        int ktimes=0;
        int price;
        int type=0;
        string adinfo="";
        if (!DateTime.TryParse(Request.Form["ctl00$ContentPlaceMainText$TextBox2"], out dtStart))
        {
            errMsg = "请选择广告发布时间";
        }
        else if (!DateTime.TryParse(Request.Form["ctl00$ContentPlaceMainText$TextBox3"], out dtEnd))
        {
            errMsg = "请选择广告发布时间";
        }
        else if (!int.TryParse(Request.Form["ctl00$ContentPlaceMainText$TextBox6"], out ktimes))
        {
            errMsg = "广告展示次数有误";
        }
        else if (ktimes < 1)
        {
            errMsg = "广告展示次数有误";
        }

        if (this.RadioButton1.Checked)
        {
            adinfo = this.RadioButton1.Text;
            type = 1;
        }
        else if (this.RadioButton2.Checked)
        {
            adinfo = this.RadioButton2.Text;
            type = 2;
        }
        else if (this.RadioButton3.Checked)
        {
            adinfo = this.RadioButton3.Text;
            type = 3;
        }
        else
        {
            errMsg = "请选择广告位置";
        }

        if (errMsg.Length > 0)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('" + errMsg + "');</script>");
            return;
        }

        price = int.Parse(adinfo.Split(" ".ToCharArray())[1].Replace("元/千人", "")) * ktimes;
        ktimes *= 1000;
        this.SqlDataSource1.UpdateCommand = "insert into [GowllADOrders]([UserID],[CID],[Type],[AdInfo],[Price],[StartDate],[EndDate],[ShowCount])values("
            + Session["ADUserID"].ToString() + "," + Label4.Text + ","+type+",'" + this.Label2.Text + "社区" + adinfo + "'," + price + ",'" + dtStart.ToString() + "','" + dtEnd.ToString() + "'," + ktimes + ")";
        this.SqlDataSource1.Update();
        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('广告已成功送入购物车。');</script>");

    }
}
